package com.font.web.zhongyao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.font.bean.Medicine;
import com.font.bean.result.Result;
import com.font.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/Medicine")
public class MedicineController {
    @Autowired
    MedicineService medicineService;

    @GetMapping("MedicineList")
    public Result prescriptionList(
            @RequestParam(value = "medicineName",required = false) String medicineName,
            @RequestParam(value = "medicineTypeKey",required = false) String medicineTypeKey,
            @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize",required = false,defaultValue = "3") Integer pageSize

    ){

        System.err.println(pageSize);
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<Medicine>()
                .eq(StrUtil.isNotBlank(medicineTypeKey),"medicine_type_key",medicineTypeKey)
                .eq(StrUtil.isNotBlank(medicineName),"medicine_name",medicineName);

        IPage<Medicine> page = medicineService.page(new Page<Medicine>(currentPage,pageSize),queryWrapper);
        int pages = page.getRecords().size();
        long pages2 = page.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("pages",pages);
        return Result.success(map);
    }
}
